package com.github.willisaway.core.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.ZipUtil;

/**
 * https://www.cnblogs.com/jpfss/p/9789311.html
 * 
 * @author liyougen
 *
 */
public class FileUtil {
    static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    public static void saveDataToFile(String path, String data) {
        BufferedWriter writer = null;
        File file = new File(path);
        File fileParent = file.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }
        // 如果文件不存在，则新建一个
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 写入
        try {
//			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
//			writer.write(data);
            // gzip 存储 2022-5-24
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(ZipUtil.gzip(data, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getDatafromFile(String path) {
        BufferedReader reader = null;
        FileInputStream fileInputStream = null;
        FileChannel channel = null;
        String laststr = "";
        try {
            fileInputStream = new FileInputStream(path);
//			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
//			reader = new BufferedReader(inputStreamReader);
//			String tempString = null;
//			while ((tempString = reader.readLine()) != null) {
//				laststr += tempString;
//			}
//			reader.close();
            // 改成gzip 方式读取
            channel = fileInputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while (channel.read(byteBuffer) > 0) {

            }
            byte[] bts = byteBuffer.array();
            laststr = ZipUtil.unGzip(bts, "UTF-8");
            // 改成gzip 方式读取 end
        } catch (IOException e) {
            logger.error("读取文件出错,路径是" + path);
            laststr = null;
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }
}
